//middleware
const isJSON = require('koa-is-json')
const zlib = require('zlib')
module.exports = options =>{
  return async function gzip(ctx,next){
    await next()
    let body = ctx.body 
    if(!body) return
    //console.log(body)
    // console.log(options.threshold)
    if(options.threshold && ctx.length < options.threshold) return
    if(isJSON(body)) body = JSON.stringify(body)
    const stream = zlib.createGzip()
    stream.end(body)
    ctx.body = stream
    ctx.set('Content-Encoding','gzip')
  }
  //压缩后再进行传入前台
}
